package com.nine.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nine.dao.PackageMapper;
import com.nine.dao.PackageitemsMapper;
import com.nine.entity.Package;
import com.nine.entity.Packageitems;
import com.nine.service.PackageService;

@Service
@Transactional
public class PackageServiceImpl implements PackageService {
	@Autowired
	private PackageMapper packageMapper;//套餐dao层组件
	@Autowired
	private PackageitemsMapper packageitemsMapper;//套餐明细组件

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED) //挂起事务
	public List<Package> getAllPackage() {
		return packageMapper.getAllPackage();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED) //挂起事务
	public List<Package> getAllPackageByCondition(Package p) {
		if(p.getPgid()!="") {
			p.setPgid("%"+p.getPgid()+"%");
		} else {
			p.setPgid(null);
		}
		if(p.getPgname()!="") {
			p.setPgname("%"+p.getPgname()+"%");
		} else {
			p.setPgname(null);
		}
		return packageMapper.getAllPackageByCondition(p);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED) //挂起事务
	public Package getPackage(String pgid) {
		return packageMapper.selectByPrimaryKey(pgid);
	}

	@Override
	public Integer addPackage(Package p) {
		return packageMapper.insertSelective(p);
	}

	@Override
	public Integer deletePackage(String pgid) {
		Integer temp1 = -1, temp2 = -1;
		List<Packageitems> list = packageitemsMapper.getPackageitemsByPgid(pgid);
		if(list.size()>0) {
			temp1 = packageitemsMapper.deleteByPgid(new String[]{pgid});
		}else {
			temp1 = 1;
		}
		if(temp1>0) {
			temp2 = packageMapper.deleteByPrimaryKey(pgid);
		}
		return temp1+temp2;
	}

	@Override
	public Integer deleteMorePackage(String[] pgid) {
		Integer temp1 = -1, temp2 = -1, count=0;
		for (int i = 0; i < pgid.length; i++) {
			List<Packageitems> list = packageitemsMapper.getPackageitemsByPgid(pgid[i]);
			if(list.size()>0) {
				count += 1;
			}
		}
		if(count>0) {
			temp1 = packageitemsMapper.deleteByPgid(pgid);
		}else {
			temp1 = 1;
		}
		if(temp1>0) {
			temp2 = packageMapper.deleteMorePackage(pgid);
		}
		return temp1+temp2;
	}

	@Override
	public Integer updatePackage(Package p) {
		return packageMapper.updateByPrimaryKeySelective(p);
	}
	
	@Override
	public Integer addPackageitems(List<Packageitems> list) {
		Integer temp=0;
		for (Packageitems items : list) {
			temp += packageitemsMapper.insertSelective(items);
		}
		return temp;
	}
	
	@Override
	public List<Packageitems> getPackageitemsByPgid(String pgid) {
		return packageitemsMapper.getPackageitemsByPgid(pgid);
	}

	@Override
	public Integer deletePackageitemsById(Integer id) {
		return packageitemsMapper.deleteByPrimaryKey(id);
	}

}
